package xyf.p10.dp;

/**
 * ClassName: P0064_MinPath
 * Description:
 * Author: xyf
 * Date: 9/9/22 10:26 AM
 * Version: 1.0
 **/
public class P0064_MinPath
{
    public int minPathSum(int[][] grid)
    {
        int[][] dp = new int[grid.length][grid[0].length];
        for (int i = 0; i < grid.length; i++)
        {
            if (i == 0)
            {
                dp[i][0] = grid[0][0];
                continue;
            }
            dp[i][0] = dp[i - 1][0] + grid[i][0];
        }

        for (int j = 1; j < grid[0].length; j++)
        {
            dp[0][j] = dp[0][j - 1] + grid[0][j];
        }

        for (int i = 1; i < grid.length; i++)
        {
            for (int j = 1; j < grid[0].length; j++)
            {
                dp[i][j] = Math.min(dp[i - 1][j] + grid[i][j], dp[i][j - 1] + grid[i][j]);
            }
        }
        return dp[grid.length - 1][grid[0].length - 1];
    }
}